import React, { PureComponent, Fragment } from 'react';
import { connect } from 'dva';
import {
  Row,
  Col,
  Card,
  Form,
  Input,
  Icon,
  Button,
  Menu,
  Dropdown, List, Progress,
} from 'antd';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import DescView from './descView';
import BidInfo from './bidInfo';
import BidAfter from './after';
import styles from '../UserManage.less';
import { powerBtnFunc } from '@/utils/utils';
import * as options from '../../../common/selectOption';
import * as option from '../../../common/options';

const FormItem = Form.Item;

/* eslint react/no-multi-comp:0 */
@connect(({ bid, loading }) => ({
  bid,
  loading: loading.models.bid,
}))

@Form.create()
class bid extends PureComponent {
  state = {
    lookDescView: false,
    afterView: false,
    bidInfoView: false,
    selectedRows: {},
    formValues: {},
    pagination: {
      pageNo: 1,
      pageSize: 15,
    },
  };


  constructor(props) {
    super(props);
  }

  componentDidMount() {
    const { dispatch } = this.props;
    dispatch({
      type: 'bid/fetch',
    });

  }

  handleStandardTableChange = (pagination, filtersArg, sorter) => {
    const { dispatch } = this.props;
    const { formValues } = this.state;

    this.setState({
      pagination: {
        pageNo: pagination.current,
        pageSize: 15,
      },
    });
    const params = {
      pageNo: pagination.current,
      pageSize: 15,
      ...formValues,
    };

    dispatch({
      type: 'bid/fetch',
      payload: params,
    });
  };
  //重置
  handleFormReset = () => {
    const { form, dispatch } = this.props;
    form.resetFields();
    this.setState({
      formValues: {},
    });
    dispatch({
      type: 'bid/fetch',
      payload: {},
    });
  };


  handleStateClick = (flag, selectedRows) => {
    const { dispatch } = this.props;
    this.setState({
      bidInfoView: flag,
    }, () => {
      if (flag) {
        dispatch({
          type: 'bid/bidInfo',
          payload: {
            code: selectedRows.current.projectNo,
          },
        });
      }
    });

  };
  handleAfterClick = (flag, selectedRows) => {
    const { dispatch } = this.props;
    this.setState({
      afterView: flag,
    }, () => {
      if (flag) {
        dispatch({
          type: 'bid/bidAfter',
          payload: {
            code: selectedRows.current.projectNo,
          },
        });
      }
    });

  };
  //搜索
  handleSearch = e => {
    e.preventDefault();

    const { dispatch, form } = this.props;

    form.validateFields((err, fieldsValue) => {
      if (err) return;

      const values = {
        ...fieldsValue,
      };
      this.setState({
        formValues: values,
      });

      dispatch({
        type: 'bid/fetch',
        payload: values,
      });
    });
  };


  //查看
  handleLookDesc = (flag, record) => {
    this.setState({
      lookDescView: !!flag,
      selectedRows: record,
    }, () => {
      if (!!flag) {
        const { dispatch } = this.props;
        dispatch({
          type: 'bid/borrowList',
          payload: { code: record.projectNo },
        });
      }
    });

  };


  renderSimpleForm() {
    const {
      form: { getFieldDecorator },
    } = this.props;

    return (
      <Form onSubmit={this.handleSearch} layout="inline">
        <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
          <Col md={8} sm={24}>
            <FormItem label="企业名称">
              {getFieldDecorator('enterpriseName')(<Input placeholder="请输入企业名称"/>)}
            </FormItem>
          </Col>
          <Col md={8} sm={24}>
            <FormItem label="标的名称">
              {getFieldDecorator('projectName')(<Input placeholder="请输入标的名称"/>)}
            </FormItem>
          </Col>
          <Col md={8} sm={24}>
        <span className={styles.submitButtons}>
        <Button type="primary" htmlType="submit">
        查询
        </Button>
        <Button style={{ marginLeft: 8 }} onClick={this.handleFormReset}>
        重置
        </Button>

        </span>
          </Col>
        </Row>
      </Form>
    );
  }

  onChange = (page, pageSize) => {
    const { dispatch } = this.props;
    const { formValues } = this.state;
    const params = {
      pageNo: page,
      pageSize: pageSize,
      ...formValues,
    };

    dispatch({
      type: 'bid/fetch',
      payload: params,
    });
  };

  render() {
    const { bid: { data, borrowList, bidInfo, afterInfo }, loading } = this.props;
    const { lookDescView, bidInfoView, afterView } = this.state;
    const paginationProps = {
      showSizeChanger: false,
      showQuickJumper: false,
      pageSize: data.pageSize ? data.pageSize : 1,
      total: data.total?data.total:1,
      onChange: this.onChange,
    };
    const MoreBtn = props => (
      <Dropdown
        overlay={
          <Menu selectedKeys={[]}>
            <Menu.Item key="state" onClick={() => this.handleStateClick(true, props)}>项目说明</Menu.Item>
            <Menu.Item key="msg" onClick={() => this.handleAfterClick(true, props)}>投后管理</Menu.Item>
          </Menu>
        }
      >
        <a>
          更多 <Icon type="down"/>
        </a>
      </Dropdown>
    );
    const ListContent = ({ data: { projectName, projectType, amount, rate, otherRate, surplusAmount, raiseAmount, percent, totalDays, status } }) => (
      <div className={styles.listContent}>

        <div className={styles.listContentItem}>
          <span>总金额</span>
          <p>{amount}</p>
        </div>
        <div className={styles.listContentItem}>
          <span>募集金额</span>
          <p>{raiseAmount}</p>
        </div>
        <div className={styles.listContentItem}>
          <span>利率</span>
          <p>{rate}%</p>
        </div>
        <div className={styles.listContentItem}>
          <span>加息</span>
          <p>{otherRate}%</p>
        </div>
        <div className={styles.listContentItem}>
          <span>项目周期</span>
          <p>{totalDays}天</p>
        </div>
        <div className={styles.listContentItem}>
          <span>剩余金额</span>
          <p>{surplusAmount}</p>
        </div>
        <div className={styles.listContentItem}>
          <span>项目类型</span>
          <p>{options.projectType.map((item) => {
            if (item.key === projectType) {
              return item.value;
            }
          })}</p>
        </div>
        <div className={styles.listContentItem}>
          <span>项目状态</span>
          <p>{option.productStatus.map((item) => {
            if (item.key === status) {
              return item.value;
            }
          })}</p>
        </div>
        <div className={styles.listContentItem}>
          <Progress
            percent={Number(percent)}
            status={status === 5 ? 'active' : 'success'}
            strokeWidth={6}
            style={{ width: 180 }}/>
        </div>
      </div>
    );
    return (
      <PageHeaderWrapper title="标的管理">
        <Card bordered={false}>

          <div className={styles.tableList}>
            <div className={styles.tableListForm}>{this.renderSimpleForm()}</div>

            <List
              size="large"
              rowKey="projectNo"
              loading={loading}
              pagination={paginationProps}
              dataSource={data.data}
              renderItem={item => (
                <List.Item
                  actions={[
                    <a key={'list'} onClick={() => this.handleLookDesc(true, item)}>
                      出借列表
                    </a>,
                    <MoreBtn current={item}/>,
                  ]}>
                  <List.Item.Meta
                    title={item.projectName}
                    description={`风险等级${item.riskLevel}`}
                    style={{ display: 'block', flex: 'none' }}
                  />
                  <ListContent data={item}/>
                </List.Item>
              )}
            />
          </div>
        </Card>
        {
          bidInfo ? <BidInfo
            bidInfoView={bidInfoView}
            bidInfo={bidInfo ? bidInfo : {}}
            handleStateClick={this.handleStateClick}
          /> : ''
        }
        <BidAfter
          afterView={afterView}
          afterInfo={afterInfo ? afterInfo : {}}
          handleAfterClick={this.handleAfterClick}/>
        <DescView
          lookDescView={lookDescView}
          borrowList={borrowList ? borrowList : {}}
          handleLookDesc={this.handleLookDesc}/>
      </PageHeaderWrapper>
    );
  }
}

export default bid;
